package com.xingshi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingshi.springbootinit.common.ErrorCode;
import com.xingshi.springbootinit.exception.ThrowUtils;
import com.xingshi.springbootinit.model.entity.Score;
import com.xingshi.springbootinit.service.ScoreService;
import com.xingshi.springbootinit.mapper.ScoreMapper;
import org.springframework.stereotype.Service;

/**
* @author Yujian Zhang
* @description 针对表【score(积分表)】的数据库操作Service实现
* @createDate 2024-03-01 16:34:03
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
    implements ScoreService{

    /**
     * 用于签到时添加积分
     * @param userId
     */
    public void checkIn(Long userId) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Score score = this.getOne(queryWrapper);
        ThrowUtils.throwIf(score == null, ErrorCode.NOT_FOUND_ERROR);
        Integer isSign = score.getIsSign();
        ThrowUtils.throwIf(isSign == 1,ErrorCode.PARAMS_ERROR,"今天已经领取过了，明天再来吧~");

        //新增积分
        Long scoreTotal = score.getScoreTotal();
        UpdateWrapper<Score> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("scoreTotal",scoreTotal + 10)
                .set("isSign",1)
                .eq("userId",userId);
        boolean result = this.update(updateWrapper);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR,"更新积分数量和签到状态失败");
    }

    /**
     * 查询积分
     * @param userId
     * @return
     */
    public Long getUserScores(Long userId) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Score score = this.getOne(queryWrapper);
        return score.getScoreTotal();
    }

    /**
     * 消耗积分
     * @param userId
     * @param scores 消耗的积分数量，根据业务需求调整
     */
    public void deductScores(Long userId, Long scores) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Score score = this.getOne(queryWrapper);
        Long scoreTotal = score.getScoreTotal();
        ThrowUtils.throwIf(scoreTotal < scores,ErrorCode.OPERATION_ERROR,"积分不足，明天再来吧~");
        UpdateWrapper<Score> updateWrapper = new UpdateWrapper();
        updateWrapper.set("scoreTotal",scoreTotal - scores);
        boolean result = this.update(updateWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR,"更新积分数量失败");
    }

    /**
     * 返还积分
     * @param userId
     * @param scores 消耗的积分数量，根据业务需求调整
     */
    public void returnScores(Long userId, Long scores) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Score score = this.getOne(queryWrapper);
        Long scoreTotal = score.getScoreTotal();
        UpdateWrapper<Score> updateWrapper = new UpdateWrapper();
        updateWrapper.set("scoreTotal",scoreTotal + scores);
        boolean result = this.update(updateWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR,"更新积分数量失败");
    }

    /**
     * 获取签到状态
     * @param userId
     * @return
     */
    public int getIsSign(Long userId) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        Score score = this.getOne(queryWrapper);
        return score.getIsSign();
    }
}




